﻿Imports System.IO
Imports Llama
Imports Telerik.WinControls
Imports Telerik.WinControls.UI

Module EDEDrawing
    ''' <summary>
    ''' 某个单元格里的字体设置
    ''' </summary>
    Private myFont As New Font(New FontFamily("Calibri"), 12.0F, FontStyle.Bold)
    ''' <summary>
    ''' 设置某个单元格的样式
    ''' </summary>
    ''' <param name="cell"></param>
    Public Sub StyleCell(cell As GridViewCellInfo)
        cell.ReadOnly = True
        'cell.Style.Font = myFont
        cell.Style.CustomizeFill = True
        cell.Style.GradientStyle = GradientStyles.Solid
        cell.Style.BackColor = Color.FromArgb(255, 165, 0) '(162, 215, 255)
    End Sub

    ''' <summary>
    ''' 初始化图纸EDE这个window
    ''' </summary>
    Public Sub Initial_EDE()
        'clear
        RadForm1.RadGridView1.Columns.Clear() : RadForm1.RadGridView2.Columns.Clear() : RadForm1.图纸EDE属性列表.Items.Clear()
        '默认的属性列
        RadForm1.RadGridView1.Columns.Add("SP_ID", "SP_ID") : RadForm1.RadGridView2.Columns.Add("SP_ID", "SP_ID")
        RadForm1.RadGridView1.Columns.Add("Name", "Name") : RadForm1.RadGridView2.Columns.Add("Name", "Name")
        RadForm1.RadGridView1.Columns.Add("Path", "Path") : RadForm1.RadGridView2.Columns.Add("Path", "Path")
        '不允许新增
        RadForm1.RadGridView1.AllowDeleteRow = False : RadForm1.RadGridView2.AllowDeleteRow = False
        RadForm1.RadGridView1.AllowAddNewRow = False : RadForm1.RadGridView2.AllowAddNewRow = False
        RadForm1.RadGridView1.AllowDragToGroup = False : RadForm1.RadGridView2.AllowDragToGroup = False
        RadForm1.RadGridView1.AllowCellContextMenu = True : RadForm1.RadGridView2.AllowCellContextMenu = True
        'RadForm1.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill : RadForm1.RadGridView2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
        RadForm1.RadGridView1.BestFitColumns() : RadForm1.RadGridView2.BestFitColumns()
        '遍历一遍图纸的所有属性
        Dim objDrawingInLoop As LMDrawing
        If ConstDrawingID = "" Then
            MessageBox.Show("当前活动的Plant Structure中无图纸")
        Else
            Try
                objDrawingInLoop = DS.GetDrawing(ConstDrawingID)   '主窗口Load时，获得的某个图纸的ID
            Catch ex As Exception
                MessageBox.Show("查询ConstDrawingID的图纸时，失败。")
                Exit Sub
            End Try

            Dim al As ArrayList = LoopItemAtts(objDrawingInLoop, "Drawing")
            al.Add("Last Modified Time" +
                   "                                                                                                         |       " +
                   "Last Modified Time")
            al.Sort()   '属性按字母排序

            For i = 0 To al.Count - 1
                RadForm1.图纸EDE属性列表.Items.Add(al.Item(i))
            Next

            RadForm1.RadGridView2.Rows.Add("批量模式")  '固定一行即可
        End If

        RadForm1.EDE.Hide()

    End Sub

    ''' <summary>
    ''' 查询一次数据
    ''' </summary>
    Public Sub LoadDrawingToEDE()
        RadForm1.RadGridView1.Rows.Clear()
        Dim objHistories As New LMHistories : Dim objDrawings As New LMDrawings : Dim objDrawing As LMDrawing
        Dim ReadOnlymask As Integer : Dim Index = 0 : Dim strAttrName As String : Dim objAttr As LMAAttribute
        Try
            DrawingCol.Clear()
            GetDwgCol(DS, DrawingCol, objDrawings)
            For Each objDrawingInLoop In objDrawings   '查询出所有勾选的图纸
                '默认的3个属性
                RadForm1.RadGridView1.Rows.Add(objDrawingInLoop.Attributes("SP_ID").value,
                                       objDrawingInLoop.Attributes("Name").Value,
                                       Mid(objDrawingInLoop.Attributes("Path").Value, 1, InStrRev(objDrawingInLoop.Attributes("Path").Value, "\") - 1))                'path是\A01\U0101\1.pid，这里把最后的\1.pid去了

                objDrawing = objDrawingInLoop

                'RadForm1.RadGridView1.Rows(Index).Cells(0).ReadOnly = True  '只读 SP_ID
                'RadForm1.RadGridView1.Rows(Index).Cells(0).Style.BackColor = Color.Orange  '背景色
                'RadForm1.RadGridView1.Rows(Index).Cells(2).ReadOnly = True  '只读 PATH
                'RadForm1.RadGridView1.Rows(Index).Cells(2).Style.BackColor = Color.Orange  '背景色
                StyleCell(RadForm1.RadGridView1.Rows(Index).Cells(0))
                StyleCell(RadForm1.RadGridView1.Rows(Index).Cells(2))

                For column = 3 To RadForm1.RadGridView1.Columns.Count - 1  '前3列属性是默认的,index 0~2被占，所以从3开始
                    strAttrName = RadForm1.RadGridView1.Columns(column).Name        'ColName
                    'If Mid(strAttrName, Len(strAttrName)) = "@" Then
                    '    '代表属性为select list
                    '    strAttrName = Mid(strAttrName, 1, Len(strAttrName) - 1)  '去掉@
                    'End If
                    objAttr = objDrawing.Attributes(strAttrName)  '这里必须搞一个attribute的对象实例，否则下面一句代码中的属性找不着

                    If strAttrName = "SP_AccessedBy" Then
                        'RadForm1.RadGridView1.Rows(Index).Cells("SP_AccessedBy").ReadOnly = True  '只读
                        'RadForm1.RadGridView1.Rows(Index).Cells("SP_AccessedBy").Style.BackColor = Color.Orange  '背景色
                        'RadForm1.RadGridView2.Rows(0).Cells("SP_AccessedBy").ReadOnly = True  '只读
                        'RadForm1.RadGridView2.Rows(0).Cells("SP_AccessedBy").Style.BackColor = Color.Orange  '背景色
                        StyleCell(RadForm1.RadGridView1.Rows(Index).Cells("SP_AccessedBy"))
                        StyleCell(RadForm1.RadGridView2.Rows(0).Cells("SP_AccessedBy"))
                        Try
                            File.Open(strPlantPath + objDrawing.Attributes("Path").Value, FileMode.Open).Close()   '尝试打开.pid文件，如果打开失败，就说明这个图纸被别人打开着
                        Catch ex As Exception
                            RadForm1.RadGridView1.Rows(Index).Cells(column).Value = objDrawing.Attributes(strAttrName).Value.ToString   '如果被打开，则给值，不如在数据库中，所有图纸的access by只是记录最后一次打开的人，即使图纸没人正在打开
                        End Try

                    ElseIf strAttrName = "Last Modified Time" Then
                        'RadForm1.RadGridView1.Rows(Index).Cells("Last Modified Time").ReadOnly = True  '只读
                        'RadForm1.RadGridView1.Rows(Index).Cells("Last Modified Time").Style.BackColor = Color.Orange  '背景色
                        'RadForm1.RadGridView2.Rows(0).Cells("Last Modified Time").ReadOnly = True  '只读
                        'RadForm1.RadGridView2.Rows(0).Cells("Last Modified Time").Style.BackColor = Color.Orange  '背景色
                        StyleCell(RadForm1.RadGridView1.Rows(Index).Cells("Last Modified Time"))
                        StyleCell(RadForm1.RadGridView2.Rows(0).Cells("Last Modified Time"))

                        objHistories = objDrawing.Histories
                        For Each objhi As LMHistory In objHistories   '也就只会循环一次
                            RadForm1.RadGridView1.Rows(Index).Cells("Last Modified Time").Value = objhi.Attributes.Item("Timestamp").Value   '这个属性不在图纸上，而是在history上
                        Next

                    Else  '其他常规的属性

                        ReadOnlymask = objAttr.ISPAttribute().Attribution.ReadOnlyMask     '0是可以修改，3是不行


                        If ReadOnlymask = 0 Then
                            '可以修改
                        ElseIf ReadOnlymask = 3 Then
                            '不可以修改
                            'RadForm1.RadGridView1.Rows(Index).Cells(column).ReadOnly = True  '只读
                            'RadForm1.RadGridView1.Rows(Index).Cells(column).Style.BackColor = Color.Orange  '背景色
                            'RadForm1.RadGridView2.Rows(0).Cells(column).ReadOnly = True  '只读
                            'RadForm1.RadGridView2.Rows(0).Cells(column).Style.BackColor = Color.Orange  '背景色
                            StyleCell(RadForm1.RadGridView1.Rows(Index).Cells(column))
                            StyleCell(RadForm1.RadGridView2.Rows(0).Cells(column))
                        End If
                        RadForm1.RadGridView1.Rows(Index).Cells(column).Value = objDrawing.Attributes(strAttrName).Value.ToString   '查询出的数值，放到对应的单元格
                    End If
                Next
                Index = Index + 1   '下一行
            Next

            'RadForm1.RadGridView2.Rows(0).Cells(0).ReadOnly = True  '只读 SP_ID
            'RadForm1.RadGridView2.Rows(0).Cells(0).Style.BackColor = Color.OrangeRed  '背景色
            ''Name不应当可以统一更新
            'RadForm1.RadGridView2.Rows(0).Cells(1).ReadOnly = True  '只读 SP_ID
            'RadForm1.RadGridView2.Rows(0).Cells(1).Style.BackColor = Color.Orange  '背景色
            'RadForm1.RadGridView2.Rows(0).Cells(2).ReadOnly = True  '只读 PATH
            'RadForm1.RadGridView2.Rows(0).Cells(2).Style.BackColor = Color.Orange  '背景色
            StyleCell(RadForm1.RadGridView2.Rows(0).Cells(0))
            StyleCell(RadForm1.RadGridView2.Rows(0).Cells(1))
            StyleCell(RadForm1.RadGridView2.Rows(0).Cells(2))
            Try
                If RadForm1.RadGridView2.Columns.IndexOf("DrawingNumber") > 0 Then
                    'RadForm1.RadGridView2.Rows(0).Cells("DrawingNumber").ReadOnly = True  '只读 DrawingNumber
                    'RadForm1.RadGridView2.Rows(0).Cells("DrawingNumber").Style.BackColor = Color.Orange  '背景色
                    StyleCell(RadForm1.RadGridView2.Rows(0).Cells("DrawingNumber"))
                End If

            Catch
                '如果没有这列的话，也不至于报错
            End Try
        Catch ex As Exception

        End Try
        'RadGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
        RadForm1.RadGridView1.Refresh()
        'DrawingList = Nothing

        objDrawings = Nothing

        objHistories = Nothing

        'Sub_end(Me)

    End Sub
    ''' <summary>
    ''' 检查Name和DrawingNumber
    ''' </summary>
    'Public Sub CheckDrawingNameUniqueWithFutureData(NameOrNumber As List(Of String))
    '    For i = 0 To NameOrNumber.Count - 1
    '        SelectStr = <string>
    '                        select SP_ID from xxxSchemaName.T_Drawing where name='sdfsdfs'
    '                    </string>.Value.Replace("sdfsdfs", NameOrNumber.Item(i))
    '        Dim dt As New DataTable
    '        MyPIDConn.PIDReadDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.ActivePlantPIDApp_User, SelectStr, dt)

    '        If dt.Rows.Count > 0 Then
    '            '如果互换了怎么办

    '        End If


    '    Next
    'End Sub

    ''' <summary>
    ''' 更新一次数据
    ''' </summary>
    Public Sub UpdateDrawingFromEDE()
        Dim myRow As Integer = 0
        Dim strAttrName As String = ""
        Dim objDrawingWithSameName As LMDrawing : Dim objDrawing As LMDrawing
        Dim columnsNeedtoClear As ArrayList = New ArrayList   '用于在统一 更新后,删除在grid2中,批量删除的字眼
        Try
            DS.BeginTransaction()  '类似于AUTOCAD二次开发的事务处理器


            '2022 01 13，增加对name和number的全局唯一性判断
            Dim NameCol As New List(Of String)
            For j = 0 To RadForm1.RadGridView1.Rows.Count - 1
                If Not NameCol.Contains(RadForm1.RadGridView1.Rows(j).Cells("Name").Value.ToString) Then
                    NameCol.Add(RadForm1.RadGridView1.Rows(j).Cells("Name").Value.ToString)   '不重复
                Else
                    '有重复了
                    MessageBox.Show("图纸的Name在列表中就有重复的，请检查。")
                    Exit Sub
                End If
            Next

            If RadForm1.RadGridView1.Columns.IndexOf("DrawingNumber") > -1 Then
                NameCol.Clear()
                For j = 0 To RadForm1.RadGridView1.Rows.Count - 1
                    If Not NameCol.Contains(RadForm1.RadGridView1.Rows(j).Cells("DrawingNumber").Value.ToString) Then
                        NameCol.Add(RadForm1.RadGridView1.Rows(j).Cells("DrawingNumber").Value.ToString)   '不重复
                    Else
                        '有重复了
                        MessageBox.Show("图纸的DrawingNumber在列表中就有重复的，请检查。")
                        Exit Sub
                    End If
                Next
            End If

            'CheckDrawingNameUniqueWithFutureData(NameCol)

            For j = 0 To RadForm1.RadGridView1.Rows.Count - 1   '遍历行（即grid1所有图纸数目）
                objDrawing = DS.GetDrawing(RadForm1.RadGridView1.Rows(j).Cells("SP_ID").Value)   '以SP_ID,来获取图纸
                Dim NewDrawingName As String = RadForm1.RadGridView1.Rows(myRow).Cells("Name").Value.ToString
                Dim OldDrawingName As String = objDrawing.Attributes("Name").Value.ToString
                If OldDrawingName <> NewDrawingName Then   '这里是独立更新,因为默认的3列属性里,就只有Name是可编辑的
                    '去判断下Name是否为唯一
                    'Name Path .pid文件的更新，单独拿出来，因为还要手动去改Path和.pid文件的名称
                    '因为在API中修改图纸名称后,对应.PID文件的名字不会变,是一个BUG,需要手动去修改
                    '可能因为没有权限啥的失败
                    Dim TempPIDName As String : Dim OldPIDFullPath As String
                    TempPIDName = strPlantPath + RadForm1.RadGridView1.Rows(myRow).Cells(2).Value + "\" + NewDrawingName + ".pid"
                    OldPIDFullPath = strPlantPath + objDrawing.Attributes("Path").Value
                    Try
                        Rename(OldPIDFullPath, TempPIDName)         '去文件夹下改.PID的文件名
                    Catch ex As Exception
                        If InStr(ex.ToString.ToLower, "found") > 0 Or InStr(ex.ToString.ToLower, "找到") > 0 Then
                            MessageBox.Show("图纸 " + objDrawing.Attributes("Name").Value + " 在共享路径内找不到。" + ex.Message())
                        ElseIf InStr(ex.ToString.ToLower, "already") > 0 Or InStr(ex.ToString.ToLower, "存在") > 0 Then
                            Writelog("图纸（“ + OldDrawingName + ”）,无法修改Name为（“ + NewDrawingName + ”）。已有其他重名的图纸存在了。", "图纸EDE")
                        Else
                            MessageBox.Show("图纸 " + objDrawing.Attributes("Name").Value + " 可能被人打开着。" + ex.Message())
                        End If
                        'Sub_end(Me)

                        GoTo Finish
                    End Try
                    strAttrName = RadForm1.RadGridView1.Columns(1).Name
                    objDrawing.Attributes(strAttrName).Value = NewDrawingName '更新Name
                    objDrawing.Commit()
                    '不会更新GlobalDrawing这个表里的Name和Number
                    strAttrName = RadForm1.RadGridView1.Columns(2).Name    '以列 Path，
                    objDrawing.Attributes(strAttrName).Value = RadForm1.RadGridView1.Rows(myRow).Cells(2).Value + "\" + NewDrawingName + ".pid"   '更新Path,因为数据库的path是 \A01\U0101\1.pid这样，但是用API查出来的是 \A01\U0101
                    objDrawing.Commit()
                    If UpdateGlobalDrawing(NewDrawingName, objDrawing.Id, "Name") Then '默认情况下,通过API,不会更新globalDrawing中的数据

                        If UpdateDrawingSite(NewDrawingName, objDrawing.Id, "Name") Then  '默认情况下,通过API,不会更新drawingsite中的数据
                            If UpdateDrawingVersion(NewDrawingName, objDrawing.Id, "Name") Then  '默认情况下,通过API,不会更新drawingsite中的数据
                            Else
                                objDrawing.Attributes("Name").Value = OldDrawingName   '还原
                                objDrawing.Commit()
                                UpdateGlobalDrawing(OldDrawingName, objDrawing.Id, "Name")
                                UpdateDrawingSite(OldDrawingName, objDrawing.Id, "Name")
                                Rename(TempPIDName, OldPIDFullPath)         '去文件夹下改.PID的文件名
                            End If

                        Else
                            objDrawing.Attributes("Name").Value = OldDrawingName   '还原
                            objDrawing.Commit()
                            UpdateGlobalDrawing(OldDrawingName, objDrawing.Id, "Name")
                            Rename(TempPIDName, OldPIDFullPath)         '去文件夹下改.PID的文件名
                            DS = New LMADataSource
                            Writelog("图纸（“ + OldDrawingName + ”）,无法修改Name为（“ + NewDrawingName + ”）。已有其他重名的图纸存在于其他单元了。", "图纸EDE")
                        End If
                    Else
                        objDrawing.Attributes("Name").Value = OldDrawingName   '还原
                        objDrawing.Commit()
                        Rename(TempPIDName, OldPIDFullPath)         '去文件夹下改.PID的文件名
                    End If

                End If

                For column = 3 To RadForm1.RadGridView1.Columns.Count - 1  '遍历列（即所有已选属性数目）,从默认3列往后开始 
                    strAttrName = RadForm1.RadGridView1.Columns(column).Name    '以列的名字，获取属性数据库名
                    If RadForm1.RadGridView1.Rows(j).Cells(column).ReadOnly Then
                        '只读的列不更新

                    ElseIf strAttrName = "DrawingNumber" Then
                        'DrawingNumber只能单独更新
                        Dim OldDrawingNumber As String = objDrawing.Attributes(strAttrName).Value.ToString
                        Dim NewDrawingNumber As String = RadForm1.RadGridView1.Rows(myRow).Cells(column).Value.ToString
                        If OldDrawingNumber.Trim <> NewDrawingNumber.Trim Then
                            objDrawing.Attributes(strAttrName).Value = NewDrawingNumber
                            objDrawing.Commit()
                            '相同就别管了
                            '这里不会去进行拼接，只是单纯对drawingNumber 进行手动的修改
                            If UpdateGlobalDrawing(NewDrawingNumber, objDrawing.Id, "DrawingNumber") Then '默认情况下,通过API,不会更新globalDrawing中的数据
                                If UpdateDrawingSite(NewDrawingNumber, objDrawing.Id, "DrawingNumber") Then  '默认情况下,通过API,不会更新drawingsite中的数据
                                    If UpdateDrawingVersion(NewDrawingNumber, objDrawing.Id, "DrawingNumber") Then  '默认情况下,通过API,不会更新drawingsite中的数据
                                    Else
                                        objDrawing.Attributes(strAttrName).Value = OldDrawingNumber   '还原
                                        objDrawing.Commit()
                                        UpdateGlobalDrawing(OldDrawingNumber, objDrawing.Id, "DrawingNumber")
                                        UpdateDrawingSite(OldDrawingNumber, objDrawing.Id, "DrawingNumber")
                                    End If
                                Else
                                    objDrawing.Attributes(strAttrName).Value = OldDrawingNumber   '还原
                                    objDrawing.Commit()
                                    UpdateGlobalDrawing(OldDrawingNumber, objDrawing.Id, "DrawingNumber")
                                End If
                            Else
                                objDrawing.Attributes(strAttrName).Value = OldDrawingNumber   '还原
                                objDrawing.Commit()
                            End If

                        End If
                    ElseIf RadForm1.RadGridView2.Rows(0).Cells(column).Value = "即将清空" Then
                        '这种情况，就默认统一更新
                        objDrawing.Attributes(strAttrName).Value = System.DBNull.Value
                        objDrawing.Commit()
                        columnsNeedtoClear.Add(column)
                        '做一个判断，批量删除的工作
                    ElseIf RadForm1.RadGridView2.Rows(0).Cells(column).Value Is Nothing Then
                        'grid2为nothing时，就默认单独更新
                        'objDrawing.Attributes(strAttrName).Value = RADGridView1.Rows(myRow).Cells(column).Value
                        UpdateDwgPropFromCell(RadForm1.RadGridView1, myRow, column, objDrawing)
                    Else
                        '正常属性

                        If RadForm1.RadGridView2.Rows(0).Cells(column).Value = "" Then
                            UpdateDwgPropFromCell(RadForm1.RadGridView1, myRow, column, objDrawing)
                        Else
                            'grid2有值时，进行统一更新
                            UpdateDwgPropFromCell(RadForm1.RadGridView2, 0, column, objDrawing)
                        End If

                    End If
                Next



                objDrawing.Commit()
                myRow = myRow + 1   '如果是单独更新，这里就step1；如果是统一更新，这里step0，即永远是0
            Next
            '这里会报错，比如违反不唯一性条例时
            DS.CommitTransaction()
        Catch ex As Exception
            'Sub_end(Me)
            MessageBox.Show(ex.Message)
            GoTo Finish
        End Try
Finish:
        'Sub_end(Me)
        MessageBox.Show("Done!")

        For i = 0 To columnsNeedtoClear.Count - 1
            RadForm1.RadGridView2.Rows(0).Cells(columnsNeedtoClear(i)).Value = ""   '清空grid2
        Next
        objDrawingWithSameName = Nothing
        myRow = Nothing
        columnsNeedtoClear = Nothing

        LoadDrawingToEDE()
    End Sub

    ''' <summary>
    ''' 设置下方的批量删除的标记
    ''' </summary>
    Sub SetBatchDeletionFlg(sender As Object, e As EventArgs)
        Dim currentCell = RadForm1.RadGridView2.CurrentCell
        If currentCell.ColumnIndex() > 0 And RadForm1.RadGridView2.Rows(0).Cells(currentCell.ColumnIndex()).ReadOnly = False Then
            RadForm1.RadGridView2.Rows(0).Cells(currentCell.ColumnIndex).Value = "即将清空"
        End If
    End Sub
    Public Function UpdateGlobalDrawing(dwgName As String, spid As String, PropName As String) As Boolean

        SelectStr = <string>
update xxxSchemaName.t_globaldrawing  
set xxxProp = 'xxxdwgName'
where sp_drawingid = 'xxxID'
            </string>.Value
        SelectStr = SelectStr.Replace("xxxdwgName", dwgName)
        SelectStr = SelectStr.Replace("xxxID", spid)
        SelectStr = SelectStr.Replace("xxxProp", PropName)
        If InStr(MyPIDConn.PIDUpdateDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.ActivePlantPIDApp_User, SelectStr), "错误信息") > 0 Then
            'error
            Return False
        Else
            'ok
            Return True
        End If
    End Function

    Public Function UpdateDrawingVersion(dwgName As String, spid As String, PropName As String) As Boolean

        SelectStr = <string>
update xxxSchemaName.T_DRAWINGVERSION  
set xxxProp = 'xxxdwgName'
where sp_drawingid = 'xxxID'
            </string>.Value
        SelectStr = SelectStr.Replace("xxxdwgName", dwgName)
        SelectStr = SelectStr.Replace("xxxID", spid)
        SelectStr = SelectStr.Replace("xxxProp", PropName)
        If InStr(MyPIDConn.PIDUpdateDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.ActivePlantPIDApp_User, SelectStr), "错误信息") > 0 Then
            'error
            Return False
        Else
            'ok
            Return True
        End If
    End Function

    Public Function UpdateDrawingSite(dwgName As String, spid As String, PropName As String) As Boolean

        SelectStr = <string>
update xxxSchemaName.t_DrawingSite  
set xxxProp = 'xxxdwgName'
where sp_drawingid = 'xxxID'
            </string>.Value
        SelectStr = SelectStr.Replace("xxxdwgName", dwgName)
        SelectStr = SelectStr.Replace("xxxID", spid)
        SelectStr = SelectStr.Replace("xxxProp", PropName)

        If InStr(MyPIDConn.PIDUpdateDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.PIDPlant_User, SelectStr), "错误信息") > 0 Then
            'error
            Return False
        Else
            'ok
            Return True
        End If
    End Function

    Sub CheckOrUncheckOneProp(ByRef e As Object)
        Dim DisName As String = Mid(e.Item.Text, 1, InStr(e.Item.Text, "|") - 1)   '用于datagridview的属性列的显示名
        Dim ColName As String = Mid(e.Item.Text, InStr(e.Item.Text, "|") + 1)       '用于datagridview的属性列的实际名
        'Dim cmbcol As New DataGridViewComboBoxColumn
        Dim objDrawingInLoop As LMDrawing : Dim objAttr As LMAAttribute

        objDrawingInLoop = DS.GetDrawing(ConstDrawingID)   '主窗口Load时，获得的某个图纸的ID

        'If RadCheckedListBox1.GetItemChecked(RadCheckedListBox1.SelectedIndex) Then
        If e.Item.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off Then
            '未被选中
            If Mid(ColName, Len(ColName)) = "@" Then
                ColName = Mid(ColName, 1, Len(ColName) - 1)  '去掉@
            End If
            On Error Resume Next    '防止没有增加过的column，被删除时的报错

            RadForm1.RadGridView1.Columns.Remove(Trim(ColName))
            RadForm1.RadGridView2.Columns.Remove(Trim(ColName))
        Else
            If Mid(ColName, Len(ColName)) = "@" Then     '0是可以修改，3是不行 Then
                '代表属性为select list
                ColName = Mid(ColName, 1, Len(ColName) - 1)  '去掉@

                objAttr = objDrawingInLoop.Attributes(Trim(ColName))

                Dim cmbcol As New GridViewComboBoxColumn
                cmbcol.HeaderText = Trim(DisName) '显示名
                cmbcol.Name = Trim(ColName)

                Dim DropDownList As New ArrayList
                'DropDownList.Columns.Add("value")
                DropDownList.Add("") '方便清空
                For i = 1 To objAttr.ISPAttribute.Attribution.ISPEnumAtts.Count   '这时候确认是select list了，没必要做判断了
                    'cmbcol.Items.Add(objAttr.ISPAttribute.Attribution.ISPEnumAtts.Item(i).Name.ToString)   '每个enum
                    'Dim dr As DataRow = DropDownList.NewRow
                    'dr("value") = objAttr.ISPAttribute.Attribution.ISPEnumAtts.Item(i).Name.ToString
                    'DropDownList.Rows.Add(dr)
                    DropDownList.Add(objAttr.ISPAttribute.Attribution.ISPEnumAtts.Item(i).Name.ToString)
                Next

                cmbcol.DataSource = DropDownList
                RadForm1.RadGridView1.Columns.Add(cmbcol)

                ' RadGridView2.Columns.Add(cmbcol)

                Dim cmbcol2 As New GridViewComboBoxColumn
                cmbcol2.HeaderText = Trim(DisName) '显示名
                cmbcol2.Name = Trim(ColName)
                DropDownList.RemoveAt(0)
                cmbcol2.DataSource = DropDownList

                'For i = 1 To objAttr.ISPAttribute.Attribution.ISPEnumAtts.Count   '这时候确认是select list了，没必要做判断了
                '    cmbcol2.Items.Add(objAttr.ISPAttribute.Attribution.ISPEnumAtts.Item(i).Name.ToString)   '每个enum
                'Next
                RadForm1.RadGridView2.Columns.Add(cmbcol2)    '不能用同一个
                cmbcol = Nothing
                cmbcol2 = Nothing

                'RadForm1.RadGridView2.Columns.Add(Trim(ColName), Trim(DisName))  '因为grid2上，右键会报错
            Else
                '被选中   非select list
                RadForm1.RadGridView1.Columns.Add(Trim(ColName), Trim(DisName))  '前一个参数是columnName，后一个参数是显示名
                RadForm1.RadGridView2.Columns.Add(Trim(ColName), Trim(DisName))  '前一个参数是columnName，后一个参数是显示名
            End If

        End If
        LoadDrawingToEDE()
        RadForm1.RadGridView1.Refresh()
        RadForm1.RadGridView2.Refresh()
    End Sub
    ''' <summary>
    ''' 更新某一个单元格的数据
    ''' </summary>
    ''' <param name="myGrid"></param>
    ''' <param name="myRow"></param>
    ''' <param name="Column"></param>
    ''' <param name="objDrawing"></param>
    Public Sub UpdateDwgPropFromCell(ByRef myGrid As RadGridView, myRow As Integer, Column As Integer, ByRef objDrawing As LMDrawing)
        Dim strAttrName As String
        strAttrName = myGrid.Columns(Column).Name
        If IsDBNull(objDrawing.Attributes(strAttrName).Value) And Not IsDBNull(myGrid.Rows(myRow).Cells(Column).Value) Then

            If myGrid.Rows(myRow).Cells(Column).Value = "" Then
                'Case 1
                'pid为dbnull，同时grid为""的话，就别提交，增加数据库的负担了
            Else
                'Case 2
                objDrawing.Attributes(strAttrName).Value = myGrid.Rows(myRow).Cells(Column).Value
                objDrawing.Commit()
            End If

        ElseIf objDrawing.Attributes(strAttrName).Value = myGrid.Rows(myRow).Cells(Column).Value Then
            'Case 4
            '一样的话，就别提交，增加数据库的负担了
        Else

            If myGrid.Rows(myRow).Cells(Column).Value <> "" Then
                'Case 5
                objDrawing.Attributes(strAttrName).Value = myGrid.Rows(myRow).Cells(Column).Value
                objDrawing.Commit()
            ElseIf myGrid.Rows(myRow).Cells(Column).Value Is Nothing Then
                'Case 3
                objDrawing.Attributes(strAttrName).Value = System.DBNull.Value
                objDrawing.Commit()
            End If
        End If
    End Sub
End Module
